package xin.yangshuai.activemqgeneral.jms.topicsubscriber;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.io.IOException;

/**
 * JmsConsumerTopicSubscriber2
 *
 * @author shuai
 * @date 2020/6/22
 */
public class JmsConsumerTopicSubscriber2 {

    public static final String ACTIVEMQ_URL = "tcp://192.168.124.7:61616";
    public static final String TOPIC_NAME = "topic-subscriber-01";

    public static void main(String[] args) throws JMSException, IOException {

        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);

        Connection connection = activeMQConnectionFactory.createConnection();
        connection.setClientID("TOPIC消费者2");

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        TopicSubscriber topicSubscriber = session.createDurableSubscriber(session.createTopic(TOPIC_NAME), "topic-subscriber");
        connection.start();

        Message message = topicSubscriber.receive();

        while (message != null) {
            TextMessage textMessage = (TextMessage) message;
            System.out.println("****TOPIC消费者2接收订阅消息-MessageListener：" + textMessage.getText());
            message = topicSubscriber.receive(1000L);
        }

        topicSubscriber.close();
        session.close();
        connection.close();

    }
}
